package me.szlx.region;

import me.szlx.thing.bundle.GenericThingBundle;
import me.szlx.thing.bundle.ThingBundle;
import me.szlx.thing.manager.AbstractThingManager;

import javax.annotation.PostConstruct;

public class RegionManager extends AbstractThingManager<Region> implements Regions.RegionManager {
    private static final String DEFAULT_REGION_RESOURCE = "regions.yml";
    private ThingBundle<Region> bundle;
    private String resource;
    private String prefix;

    public RegionManager(ThingBundle<Region> bundle) {
        this.bundle = bundle;
    }

    public RegionManager() {
    }

    public RegionManager(String resource) {
        this.resource = resource;
    }

    private ThingBundle<Region> createBundle(String resource) {
        GenericThingBundle<Region> result = new GenericThingBundle(resource);
        result.setPrefix(prefix);
        result.setThingCreator(Region.Creator.newInstance());
        return result;
    }

    @Override
    protected ThingBundle<Region> getThingBundle() {
        if (bundle == null) {
            this.bundle = createBundle(resource != null ? resource : DEFAULT_REGION_RESOURCE);
        }
        return bundle;
    }

    @PostConstruct
    public void register() {
        Regions.RegionManager.use(this);
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }
}
